草庐IT

Java final 与 C++ const

全部标签

c++ - C++ 中 Main 参数的 Const-Qualification

C++标准要求所有符合规范的实现都支持main的以下两个签名:intmain();intmain(int,char*[]);如果是后一种签名,添加(顶级)const是否会破坏任何语言规则?例如:intmain(constintargc,char**constargv);据我了解,顶级const限定不会影响函数的签名哈希,因此就规范而言它应该是合法的。此外,是否有人遇到过拒绝此类修改的实现? 最佳答案 这是一个knownissue在标准中。另见thisusenetdiscussion关于这个话题。

c++ - 为什么函数参数中的 const 限定符用于重载解析?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:FunctionswithconstargumentsandOverloading我对重载和const声明规则感到很困惑。这里有两件让我困惑的事情,也许你能帮助我找到我头脑中更深层次的误解,这些误解导致我对它们感到困惑。;)第一期:我的编译器允许这样做:voidf(int&x){std::cout但是下面会导致编译错误(函数已经有主体):voidf(intx){std::cout我想这是有道理的,因为我认为const只是告诉编译器传递的对象没有改变,在第二种情况下它无论如何都会被复制。但如果这是正确的,那么为

c++ - 当一个方法只接受 Foo *const 时,我应该 const_cast "this"吗?

我有一个类Foo这是一个self引用的树状结构(最低限度):classFoo{public://Getsthischild'spositionrelativetoit'sparent.intgetPosition()const{returnparent->indexOf(this);}intindexOf(constFoo*constchild)const{returnchildren.indexOf(child);//thislinecausesanerror.}private:Foo*parent;QListchildren;}行returnchildren.indexOf(chi

c++ - 丢失 const volatile 限定词

我有一个编译错误:errorC3848:expressionhavingtype'constunicode::endian_swap'wouldlosesomeconst-volatilequalifiersinordertocall'unsignedlongunicode::endian_swap::operator()(T&)'此错误的描述,Here,并没有真正解释发生了什么。我无法在较小的示例中重现错误,但我可以展示我的类的基本布局。templatestructendian_swap{endian_swap(void){}Toperator()(T&_val)const{retu

c++ - const 和 constexpr 数组之间的区别

为什么const和constexpr与数组一起使用时有区别?intconstxs[]{1,2,3};constexprintys[]{1,2,3};intas[xs[0]];//error.intbs[ys[0]];//fine.我希望xs[0]和ys[0]都是常量表达式,但只有后者被这样对待。 最佳答案 作为社区wiki的更长的评论。表达式xs[0]在[expr.sub]/1中定义为*((xs)+(0))。(请参阅下面的括号。)Oneoftheexpressionsshallhavethetype“pointertoT”andth

c++ - 在 clang 中强制使用 `const char[]` 字符串文字

编译以下代码voidf(char*,constchar*,...){}voidf(constchar*,...){}intmain(){f("a","b");}用clang给我这个错误:prog.cpp:6:2:error:callto'f'isambiguousf("a","b");^prog.cpp:1:6:note:candidatefunctionvoidf(char*,constchar*,...){}^prog.cpp:2:6:note:candidatefunctionvoidf(constchar*,...){}^AFAIK字符串字面量在C++中是常量,因此重载规则应该

c++ - lambda 的 const auto 和 auto 之间的区别

有什么(有用的?)区别:autotest=[..](..){..};和constautotest=[..](..){..};? 最佳答案 是的,如果lambda被声明为mutable,那么您不能在第二种情况下调用它。intx=0;constautotest=[x]()mutable{++x;};test();//error 关于c++-lambda的constauto和auto之间的区别,我们在StackOverflow上找到一个类似的问题: https://

c++ - 在 lambda 闭包中复制的 const 对象不可变

我正在尝试通过(可变)lambda中的拷贝来捕获const对象。然而,我的编译器提示说,捕获的对象是常量。难道不能将对象复制为非常量吗?structFoo{Foo(){}voidFunc(){}};intmain(){constFoofoo;[foo]()mutable{foo.Func();};}用g++4.7.2编译:testcase.cpp:Inlambdafunction:testcase.cpp:10:29:error:nomatchingfunctionforcallto‘Foo::Func()const’testcase.cpp:10:29:note:candidatei

c++ - std::set::iterator 和 std::set::const_iterator 之间是否存在操作差异?

对于大多数容器,iteratortype提供对容器中值的读写访问,const_iterator类型提供只读访问。但是,对于std::set,迭代器类型无法提供读写访问,因为修改集合中的值(可能)会破坏容器不变量。因此,在std::set,两者iterator和const_iterator提供只读访问权限。这引出了我的问题:使用std::set::iterator可以做的事情之间有什么区别吗?以及你可以用std::set::const_iterator做的事情?请注意,在C++11中,容器的操作方法(例如erase)可以采用const_iterator。参数。

c++ - 如果在声明时未初始化,那么在 c 中使用 const 值有什么用?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:constinCvsconstinC++我有以下代码在C中intmain(){constintk;//allowedbutgarbageandlaterwecan'tmodifyprintf("%d",k);}o/p=Garbage在C++中intmain(){constintk;//notallowedfromhereitselfprintf("%d",k);}o/p-compiletimeerror如果允许在没有初始化的情况下声明它但在它之后,我怀疑C中const的用途是什么声明我们不能初始化它。但是对于